import os
from socket import AddressFamily

import psutil

def get_ip_address_by_nic(nic_name):
    #如果nic_name 为空，从环境变量获取 ，环境变量NETWORK_NAME
    nic_name = nic_name or os.getenv('NETWORK_NAME', 'eth1')
    addrs = psutil.net_if_addrs()
    if nic_name in addrs:
        print(nic_name)
        for addr in addrs[nic_name]:
            if addr.family == AddressFamily.AF_INET:  # 获取IPv4地址
                return addr.address
    return "127.0.0.1"

if __name__ == "__main__":
    # nic_name = "en0"  # 替换为你要查询的网卡名称
    ip_address = get_ip_address_by_nic(None)
    if ip_address:
        print(f"IP Address of: {ip_address}")
    else:
        pass
        # print(f"No IP address found for {nic_name}")
